﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XiaoYuStock.Core;
namespace XiaoYuStock.WebApp.App_Start
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
    public class CustomHandleErrorAttribute : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
            SaveExceptionAndError(filterContext);
            base.OnException(filterContext);
        }
        private void SaveExceptionAndError(ExceptionContext exceptionContext)
        {
            string errortime = string.Empty;
            string erroraddr = string.Empty;
            string errorinfo = string.Empty;
            string errorsource = string.Empty;
            string errortrace = string.Empty;
            errortime = "发生时间: " + System.DateTime.Now.ToString();
            erroraddr = "异常位置: " + exceptionContext.RequestContext.HttpContext.Request.Url.ToString();
            errorinfo = "异常信息: " + exceptionContext.Exception.Message;
            errorsource = "错误源:" + exceptionContext.Exception.Source;
            errortrace = "堆栈信息:" + exceptionContext.Exception.StackTrace;
            //日志登记操作
            try
            {
                string logStr = "\n====================================ERROR BGEIN============================================ \n";
                logStr = logStr + errortime + "\n" + erroraddr + "\n" + errorinfo + "\n" + errorsource + "\n" + errortrace + "\n";
                logStr = logStr + "======================================ERROR END========================================== \n";
                LogHelper.ErrorAsync(logStr);
                // 写入日志 操作
            }
            catch
            {
            }
            finally
            {
            }
        }


    }
}